package com.aotao.springboot.controller;

import com.aotao.springboot.Tools.ArticleTools;
import com.aotao.springboot.domain.Article;
import com.aotao.springboot.domain.ArticleTag;
import com.aotao.springboot.domain.ArticleType;
import com.aotao.springboot.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleTools articleTools;

    @RequestMapping("/toIndex")
    public String toIndex(Model model){
        List<Article> articles = articleService.getArticle();
        model.addAttribute("articles",articles);
        return "admin/index";
    }

    @RequestMapping("/toLogin")
    public String toLogin(Model model){
        return "admin/login";
    }

    @RequestMapping("/toBlogs-input")
    public String toBlogsInput(Integer articleID,Model model) {
        try {
            Article article = articleService.queryArticleByID(articleID);
            model.addAttribute("article",article);
            model.addAttribute("update","更新");
            model.addAttribute("updateAndPublish","更新并发布").addAttribute("publishToken",1);
        } catch (NullPointerException e){
            Article article = new Article();
            model.addAttribute("article",article);
            model.addAttribute("update","保存");
            model.addAttribute("updateAndPublish","发布").addAttribute("publishToken",0);
        }
        List<ArticleType> articleTypes = articleService.queryAllTypes();
        List<ArticleTag> articleTags = articleService.queryAllTags();
        model.addAttribute("articleTags",articleTags);
        model.addAttribute("articleTypes",articleTypes);
        return "admin/blogs-input";
    }

    @RequestMapping("/blogs-input")
    public String insert_blog(String labelID, Article article,Integer publishToken){
        //获取创建的时间
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd ':' HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        String createTime = formatter.format(date);
        String updateTime = formatter.format(date);
        article.setCreateTime(createTime);
        article.setUpdateTime(updateTime);
        System.out.println("article>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+article);
        //发布文章
        if (publishToken == 0) {
            articleService.insertArticle(article);

            String[] split = labelID.split(",");
            System.out.println("s1>>>>>>>>>>>>"+article.getArticleID());
            for (String s : split) {
                ArticleTag s1 = articleService.queryTagByName(s);

                articleService.insertArticleTag(article,s1);
            }
        }

        //更新文章
        if (publishToken == 1){
            System.out.println(article);
            articleService.deleteArticleToTagByArticleID(article.getArticleID());
            articleService.updateArticle(article);
            String[] split = labelID.split(",");
            articleService.deleteArticleToTagByArticleID(article.getArticleID());
            System.out.println("id>>>>>>>>>>>>"+article.getArticleID());
            for (String s : split) {
                ArticleTag s1 = articleService.queryTagByName(s);
                articleService.insertArticleTag(article,s1);
            }
        }

        //插入标签


        return "redirect:/admin/toBlogs-input";
    }

    @RequestMapping("/toBlogs")
    public String toBlogs(Model model) {
        List<Article> articles = articleService.getArticle();
        System.out.println(articles);
        model.addAttribute("articles",articles);
        return "admin/blogs";
    }

    @RequestMapping("/blogs")
    public String blogs(){

        return "redirect:admin/toBlogs";
    }

    //进入分类管理页面
    @RequestMapping("/toTypes")
    public String toTypes(Model model) {
        List<ArticleType> articleTypes = articleService.queryAllTypes();
        model.addAttribute("types",articleTypes);
        return "admin/types";
    }

    //进入新增页面
    @RequestMapping("/toTypes-input")
    public String toTypesInput(Model model, Integer typeID) {
        if (typeID == null) {
            model.addAttribute("button","增加").addAttribute("typeID",null);
        }else {
            model.addAttribute("button","更新").addAttribute("typeID",typeID);
        }
        return "admin/types-input";
    }

    //处理新增类型提交
    @RequestMapping("/types-input")
    public String typesInput(Model model,ArticleType articleType, Integer typeID) {
        if (typeID == null) {
            articleService.insertTypes(articleType);
        } else {
            System.out.println(articleType);
            articleService.updateType(articleType);
        }
        return "redirect:/admin/toTypes-input";
    }

    //删除类型
    @RequestMapping("/del-type")
    public String delType(Model model, Integer typeID) {
        articleService.updateArticleForType(typeID);
        articleService.deleteTypeByID(typeID);
        return "redirect:/admin/toTypes";
    }
}
